/**
* @CopyRight tstudy.com.cn
* @author wuyahui
* @E-mail wuyahui0124505@163.com
* @version 1.0.0
* @Date  2015年4月3日 下午1:03:40
*/
package com.tstudy.laoshibang.community;

import java.util.ArrayList;
import java.util.List;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.annotations.res.StringRes;
import org.apache.http.Header;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.assist.ImageLoadingListener;
import com.tstudy.laoshibang.R;
import com.tstudy.laoshibang.base.BaseActivity;
import com.tstudy.laoshibang.base.BaseApplication;
import com.tstudy.laoshibang.base.BaseFragment;
import com.tstudy.laoshibang.base.BaseOnScrollListener;
import com.tstudy.laoshibang.base.BaseOnScrollListener.FirstPositionCallBack;
import com.tstudy.laoshibang.base.BaseOnScrollListener.LoadMoreCallBack;
import com.tstudy.laoshibang.base.CONSTANT;
import com.tstudy.laoshibang.base.CONSTANT.LoadType;
import com.tstudy.laoshibang.base.HttpManager;
import com.tstudy.laoshibang.base.HttpManager.LoadImageViewResponse;
import com.tstudy.laoshibang.base.IndexActivity;
import com.tstudy.laoshibang.camera.CommunityPublishActivity;
import com.tstudy.laoshibang.community.CommunityListAdapter.AdapterCallBack;
import com.tstudy.laoshibang.event.ChangeGroupEvent;
import com.tstudy.laoshibang.event.CommentDeleteEvent;
import com.tstudy.laoshibang.event.CommunityCommentEvent;
import com.tstudy.laoshibang.event.CommunityLoveEvent;
import com.tstudy.laoshibang.manager.CommunityMessageManager;
import com.tstudy.laoshibang.mode.Community;
import com.tstudy.laoshibang.mode.CommunityMessage;
import com.tstudy.laoshibang.mode.Idxe;
import com.tstudy.laoshibang.mode.request.Conds;
import com.tstudy.laoshibang.mode.response.BaseResponse;
import com.tstudy.laoshibang.mode.response.CommunityListResponse;
import com.tstudy.laoshibang.profile.MyCommunityFragment;
import com.tstudy.laoshibang.utils.CommonUtil;
import com.tstudy.laoshibang.utils.LogUtil;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnDismissListener;
import android.graphics.Bitmap;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import de.greenrobot.event.EventBus;
import uk.co.senab.photoview.PhotoView;
@SuppressLint("InflateParams")
@EFragment(R.layout.community_list)
public class CommunityListFragment extends BaseFragment{
	final static String TAG = "community_list";
	public CommunityListFragment() {
		super();
	}
	
	@ViewById(R.id.community_list_root)
	RelativeLayout mRootLayout;
	
	@ViewById(R.id.community_list_new_message_layout)
	RelativeLayout mMessageLayout;
	
	@ViewById(R.id.community_list_empty_layout)
	RelativeLayout mEmptyLayout;
	
	@ViewById(R.id.community_list_title)
	TextView mTitleTxt;
	
	@ViewById(R.id.community_list_new_message_avatar)
	ImageView mMessageAvatar;
	
	@ViewById(R.id.community_list_new_message_txt)
	TextView mMessageTxt;
	
	
	@ViewById(R.id.community_list_progress)
	RelativeLayout mProgressLayout;
	
	@ViewById(R.id.community_list_list)
	PullToRefreshListView mPullToRefreshListView;
	
	BaseOnScrollListener mBaseOnScrollListener;
	LoadType mLoadType;
	
	boolean mIsFirstLoad = true;
	Handler mHandler;
	LayoutInflater mInflater;
	CommunityListAdapter mCommunityListAdapter;
	int mCurrentPosition = 0;
	int mCurrentPicturePosition = 0;
	List<Community> mCommunitys = new ArrayList<Community>();
	List<Community> mtopList = new ArrayList<Community>();
	int mTopSize;
	@StringRes(R.string.select_all)
	String mSelectAllStr;
	int mSelectAllKey = -1;
//	int mPosition;

	public static void add(int containerViewId) {
		Bundle args = new Bundle();
		args.putInt(CONSTANT.ARGS.FRAGMENT_ID, containerViewId);
		Fragment fragment = ((BaseActivity)BaseApplication.mCurrentActivity).getFragmentByTag(TAG);
		if(fragment == null){
			fragment = Fragment.instantiate(BaseApplication.mContext,CommunityListFragment_.class.getName(),args);
		}
		((BaseActivity)BaseApplication.mCurrentActivity).addInTab(containerViewId,fragment,TAG);	
	
	}
	@Override
	public void onCreate(Bundle savedInstanceState) {
		mHandler = new Handler();
		EventBus.getDefault().register(this);
		super.onCreate(savedInstanceState);
	}
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		mInflater = inflater;
		super.onCreateView(inflater, container, savedInstanceState);
		LogUtil.d("--mBaseLayout==" + mRootLayout);
		mIsFirstLoad = mRootLayout == null;
		if (mRootLayout != null) {
			((ViewGroup) mRootLayout.getParent()).removeAllViews();
		}
		return mRootLayout;
	}
	
	@AfterViews
	public void initView(){
		if (mIsFirstLoad) {
			if (BaseApplication.mCurrentGroup != null) {
				mTitleTxt.setText(BaseApplication.mCurrentGroup.name +CONSTANT.GROUP_SEPEREATE+ BaseApplication.getResString(R.string.community_list_title));
			}
			if (BaseApplication.checkLogin()) {
				List<CommunityMessage> communityMessageList  = CommunityMessageManager.getInstance().queryUnRead();
				if (communityMessageList != null && communityMessageList.size() > 0) {
					int size = communityMessageList.size();
					for (int i = 0; i < size; i++) {
						communityMessageList.get(i).parseObject();
					}
					setMessageView(communityMessageList.get(0));
				}
			}
			mCommunityListAdapter = new CommunityListAdapter();
			mPullToRefreshListView.setAdapter(mCommunityListAdapter);
			mCommunityListAdapter.setAdatperCallBack(new AdapterCallBack() {
				@Override
				public void onAvatorClick(String userNo) {
//					UserCommunityFragment.add(userNo, mFragmentId);
					MyCommunityFragment.add(mFragmentId, userNo, null);
				}
				@Override
				public void onLoveClick(Community community, int position,TextView view) {
					loveAction(community,CONSTANT.ASSO_TYPE_COMMUNITY, community.isPraise==1?CONSTANT.LOVE_ACTION:CONSTANT.UNLOVE_ACTION,view);
				}

				@Override
				public void onCommunityClick(int communityId, int position) {
					String userNo = mCommunitys.get(position).user != null?mCommunitys.get(position).user.userNo:null;
					CommunityDetailActivity.show(!TextUtils.isEmpty(mCommunitys.get(position).title), communityId, userNo,true,position);
//					if (TextUtils.isEmpty(mCommunitys.get(position).title)) {
//						CommunityDetailFragment.add(mFragmentId,communityId,userNo);
//					}else{//官方吐槽
//						OfficialCommunityDetailFragment.add(mFragmentId,communityId,userNo);
//					}
				}
				
				@Override
				public void onItemClick(int communityId,int position){
					String userNo = mCommunitys.get(position).user != null?mCommunitys.get(position).user.userNo:null;
					CommunityDetailActivity.show(!TextUtils.isEmpty(mCommunitys.get(position).title), communityId, userNo,false,position);
				}

				@Override
				public void onShareClick(int communityId, int position) {
//					showShareDialog();
					share(mCommunitys.get(position));
				}
				@Override
				public void onDeleteClick(int communityId, int position) {
					String title = BaseApplication.getResString(R.string.delete_community_dialog_title);
					String content = BaseApplication.getResString(R.string.delete_community_dialog_content);
					showDeleteCommunityDialog(title, content,communityId,position);
				}
				@Override
				public void onPicClick(Community community, int position) {
					showPictureDialog(community, position);
				}
			});
			mLoadType = LoadType.load_first;
			mHandler.postDelayed(new Runnable() {
				@Override
				public void run() {
					getCommunityList(1);
				}
			}, CONSTANT.LOAD_DATA_DELAY_TIME);
			mBaseOnScrollListener = new BaseOnScrollListener(new LoadMoreCallBack() {
				@Override
				public void loadMore() {
					mPullToRefreshListView.startLoadMore();
					mLoadType = LoadType.load_more;
					getCommunityList(0);
				}
			});
			mBaseOnScrollListener.setFirstPositionCallBack(new FirstPositionCallBack() {
				@Override
				public void firstPosition(int firstPosition) {
					mCurrentPosition = firstPosition;
				}
			});
			mPullToRefreshListView.setOnScrollListener(mBaseOnScrollListener);
			mPullToRefreshListView.setOnRefreshListener(new OnRefreshListener<ListView>() {
				@Override
				public void onRefresh(PullToRefreshBase<ListView> refreshView) {
					mLoadType = LoadType.load_refresh;
					getCommunityList(1);
				}
			});
		}
	}
	
	@Click(R.id.community_list_title_opengroup)
	public void showGroupPop() {
		((IndexActivity)getActivity()).showGroupPop();
	}
	
	Dialog mDeleteDialog;
	@SuppressLint("InflateParams")
	public void showDeleteCommunityDialog(String title,String content,final int communityId,final int position){
		RelativeLayout dialogView = (RelativeLayout) mInflater.inflate(R.layout.common_alert_dialog, null);
		if(mDeleteDialog != null && mDeleteDialog.isShowing()){
			return;
		}
		mDeleteDialog = buildAlertDialog(dialogView,CommonUtil.dip2px(80));
		TextView titleTxt = (TextView) dialogView.findViewById(R.id.common_alert_dialog_title);
		final TextView contentTxt = (TextView) dialogView.findViewById(R.id.common_alert_dialog_content);
		TextView cancelTxt = (TextView) dialogView.findViewById(R.id.common_alert_dialog_cancel);
		TextView confirmTxt = (TextView) dialogView.findViewById(R.id.common_alert_dialog_confirm);
		confirmTxt.setText(R.string.common_delete);
		titleTxt.setText(title);
		contentTxt.setText(content);
		confirmTxt.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				mDeleteDialog.dismiss();
				deleteCommunity(communityId,position);
			}
		});
		cancelTxt.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				mDeleteDialog.dismiss();
			}
		});
		mDeleteDialog.show();
	}
	
	private void deleteCommunity(int communityId,final int position){
		BaseListJsonHandler<BaseResponse> response = new BaseListJsonHandler<BaseResponse>() {
			public void onStart() {
			}
			@Override
			public void onSuccess(int statusCode, Header[] headers,
					String rawJsonResponse, BaseResponse response) {
				super.onSuccess(statusCode, headers, rawJsonResponse, response);
				if (CommonUtil.responseSuccess(response)) {
					backAction(mFragmentId);
					mCommunitys.remove(position);
					mCommunityListAdapter.setData(mCommunitys);
					int selection = Math.min(mCurrentPosition, mCommunitys.size());
					mPullToRefreshListView.getRefreshableView().setSelection(selection);
				}else {
					BaseApplication.showToast(response.getErrMsg());
				}
				afterLoading();
			}
			@Override
			public void onFailure(int statusCode, Header[] headers,
					Throwable throwable, String rawJsonData,
					BaseResponse errorResponse) {
				super.onFailure(statusCode, headers, throwable, rawJsonData, errorResponse);
			}
			@Override
			protected BaseResponse parseResponse(String rawJsonData, boolean isFailure)
					throws Throwable {
//				afterLoading();
				return mGson.fromJson(rawJsonData,BaseResponse.class);
			}
		};
		HttpManager.getInstance().deleteCommunity(BaseApplication.mUserNo, communityId, BaseApplication.mCurrentGroup.groupId, response);
	}
	@Click(R.id.community_list_publish)
	public void actionClick(){
//		CommunityPublishFragment.add(mFragmentId);
		CommunityPublishActivity.show();
	}
	
	/**
	 * 活动点赞
	 * @param activeId
	 * @param type
	 * @param isPraise
	 */
	private void loveAction(final Community community,int type,final int isPraise,final TextView loveCountTxt){
		BaseListJsonHandler<CommunityListResponse> response = new BaseListJsonHandler<CommunityListResponse>() {
			public void onStart() {
			}
			@Override
			public void onSuccess(int statusCode, Header[] headers,
					String rawJsonResponse, CommunityListResponse response) {
				super.onSuccess(statusCode, headers, rawJsonResponse, response);
				if (CommonUtil.responseSuccess(response)) {
				}
			}
			@Override
			public void onFailure(int statusCode, Header[] headers,
					Throwable throwable, String rawJsonData,
					CommunityListResponse errorResponse) {
				super.onFailure(statusCode, headers, throwable, rawJsonData, errorResponse);
			}
			@Override
			protected CommunityListResponse parseResponse(String rawJsonData, boolean isFailure)
					throws Throwable {
//				afterLoading();
				return mGson.fromJson(rawJsonData,CommunityListResponse.class);
			}
		};
		HttpManager.getInstance().loveAction(BaseApplication.mUserNo, type, community.tcId, isPraise,null, response);
	}

	private void getCommunityList(int isTop){
		if (mIsLoading) {
			return ;
		}
		if (mLoadType == LoadType.load_more) {
			start += pageSize;
		}else{
			start = 0;
			limit = pageSize;
		}
		mIsLoading = true;
		BaseListJsonHandler<CommunityListResponse> response = new BaseListJsonHandler<CommunityListResponse>() {
			public void onStart() {
				if (mLoadType == LoadType.load_first) {
					showProgressBar(mProgressLayout);
				}
			}
			@Override
			public void onSuccess(int statusCode, Header[] headers,
					String rawJsonResponse, CommunityListResponse response) {
				super.onSuccess(statusCode, headers, rawJsonResponse, response);
				if (CommonUtil.responseSuccess(response)) {
					if (response.getData() != null) {
						if (mLoadType != LoadType.load_more) {
							mCommunitys.clear();
						}
						if (response.getData().getTop() != null) {
							mtopList = response.getData().getTop();
							mTopSize = mtopList.size();
							mCommunitys.addAll(mtopList);
							mCommunityListAdapter.mTopCount = mtopList.size();
						}
						
						List<Community> list = response.getData().getList();
						if (list != null) {
							mCommunitys.addAll(list);
						}
						LogUtil.d("--mCommunitys.size==" + mCommunitys.size() + ",--list.size==" + list.size());
						if (mLoadType == LoadType.load_more) {
							if (mCommunitys.size() - mTopSize == response.getData().getCount()) {
								mBaseOnScrollListener.mIsEnd = true;
							}
						}else{
							mBaseOnScrollListener.mIsEnd = false;
						}
						if (mCommunitys.size() > 0) {
							mEmptyLayout.setVisibility(View.GONE);
						}else{
							mEmptyLayout.setVisibility(View.VISIBLE);
						}
						mCommunityListAdapter.setData(mCommunitys);
					}
					
				}else {
					BaseApplication.showToast(response.getErrMsg());
				}
				afterLoading();
			}
			@Override
			public void onFailure(int statusCode, Header[] headers,
					Throwable throwable, String rawJsonData,
					CommunityListResponse errorResponse) {
				afterLoading();
				super.onFailure(statusCode, headers, throwable, rawJsonData, errorResponse);
			}
			@Override
			protected CommunityListResponse parseResponse(String rawJsonData, boolean isFailure)
					throws Throwable {
				mIsLoading = false;
				hideProgressBar(mProgressLayout);
				return mGson.fromJson(rawJsonData,CommunityListResponse.class);
			}
		};
		List<Conds> conds = new ArrayList<Conds>();
//		if (BaseApplication.mCurrentGroup != null) {
//			conds.add(new Conds("groupId",BaseApplication.mCurrentGroup.groupId));
//		}
		HttpManager.getInstance().getCommunityList(BaseApplication.mUserNo,start,limit, null,conds,isTop,response);
		
	}

	public void afterLoading(){
		mIsLoading = false;
		hideProgressBar(mProgressLayout);
		if(mLoadType == LoadType.load_more){
			if (mBaseOnScrollListener.mIsEnd) {
				mPullToRefreshListView.endLoadMore(BaseApplication.getResString(R.string.community_no_more_data));
			}else{
				mPullToRefreshListView.stopLoadMore();
			}
		}
		if(mPullToRefreshListView != null){
			new Handler().postDelayed(new Runnable() {
				@Override
				public void run() {
					if(mPullToRefreshListView != null){
						mPullToRefreshListView.onRefreshComplete();
						LogUtil.d("--onRefreshComplete afterloading");
					}
				}
			}, 1000);
		}
	}
	public void onEventMainThread(Object object) {//发布成功后更新列表
		if (object instanceof CommunityMessage) {
			setMessageView((CommunityMessage)object);
		}else if (object instanceof String) {
			mLoadType = LoadType.load_first;
			getCommunityList(1);
		}else if(object instanceof CommunityLoveEvent){
			CommunityLoveEvent event = (CommunityLoveEvent) object;
			mCommunitys.get(event.position).isPraise = event.isPrised;
			if (event.isPrised == CONSTANT.IS_PRISED) {
				mCommunitys.get(event.position).praiseNum++;
			}else{
				mCommunitys.get(event.position).praiseNum--;
			}
			mCommunityListAdapter.setData(mCommunitys);
		}else if(object instanceof CommentDeleteEvent){
			CommentDeleteEvent event = (CommentDeleteEvent) object;
			mCommunitys.get(event.position).isPraise = event.isPrised;
			mCommunitys.get(event.position).evalNum--;
			mCommunityListAdapter.setData(mCommunitys);
		}else if(object instanceof CommunityCommentEvent){
			CommunityCommentEvent event = (CommunityCommentEvent) object;
			mCommunitys.get(event.position).evalNum++;
			mCommunityListAdapter.setData(mCommunitys);
		}else if(object instanceof ChangeGroupEvent){
			mLoadType = LoadType.load_first;
			if (BaseApplication.mCurrentGroup != null) {
				mTitleTxt.setText(BaseApplication.mCurrentGroup.name +CONSTANT.GROUP_SEPEREATE+ BaseApplication.getResString(R.string.community_list_title));
			}
			getCommunityList(1);
		}
	}
	
	public void setMessageView(CommunityMessage each){
		mMessageLayout.setVisibility(View.VISIBLE);
		mMessageTxt.setText(each.tcUreadNum + "条新消息");
		if (each.user != null) {
			HttpManager.getInstance().loadEditAvatar(mMessageAvatar,each.user.imgIdxName,R.drawable.profile_avatar_default);
		}
		mMessageLayout.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				mMessageLayout.setVisibility(View.GONE);
				((IndexActivity)getActivity()).showMessageIcon(0);
				CommunityMessageListFragment.add(BaseApplication.mUserNo,true,mFragmentId);
			}
		});
	}
	Dialog mPicDialog;
	HackyViewPager mViewPager;
	RelativeLayout mLoadingLayout;
	LinearLayout mLinearLayout;
	ImageView mDownloadView;
	ListItemPageAdapter mPageAdapter;
	public void showPictureDialog(final Community community,int position){
		RelativeLayout pictureLayout = (RelativeLayout) mInflater.inflate(R.layout.picture_view, null);
		if(mPicDialog != null && mPicDialog.isShowing()){
			return;
		}
		mPicDialog = new Dialog(BaseApplication.mCurrentActivity, R.style.fullscreendialog);
		mPicDialog.setCanceledOnTouchOutside(true);
		mLoadingLayout = (RelativeLayout)pictureLayout.findViewById(R.id.picture_view_progress);
		mPicDialog.setContentView(pictureLayout);
		mLinearLayout = (LinearLayout) pictureLayout.findViewById(R.id.picture_view_select_layout);
		mViewPager = (HackyViewPager)pictureLayout.findViewById(R.id.picture_view_viewpage);
		mDownloadView = (ImageView)pictureLayout.findViewById(R.id.picture_view_download);
		mDownloadView.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				HttpManager.getInstance().downLoadImage(community.idxes.get(mCurrentPicturePosition).imgIdxName, new LoadImageViewResponse() {
					@Override
					public void loadFinish(Bitmap bitmap) {
						String filePath = CommonUtil.saveImageToGallery(BaseApplication.mContext,bitmap);
						BaseApplication.showToast(BaseApplication.getResString(R.string.save_file_path) + filePath);
					}
				});
			}
		});
		mPageAdapter = new ListItemPageAdapter(community.idxes, position);
		mViewPager.setAdapter(mPageAdapter);
		mViewPager.setOnPageChangeListener(mPageAdapter);
		fillPictures(community.idxes, position);
		Window window = mPicDialog.getWindow();
		window.setBackgroundDrawable(new ColorDrawable(0x00000000));
		window.setLayout(BaseApplication.mScreenWidth, BaseApplication.mScreenHeight);
		window.setGravity(Gravity.CENTER);
		window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN
				| WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
		mPicDialog.show();
		mPicDialog.setOnDismissListener(new OnDismissListener() {
			@Override
			public void onDismiss(DialogInterface dialog) {
				if (mLinearLayout != null) {
					mLinearLayout.removeAllViews();
				}
			}
		});
	}
	
	class ListItemPageAdapter extends PagerAdapter implements OnPageChangeListener{
		List<Idxe> list;
		int position;
		public void setData(){
			notifyDataSetChanged();
		}
		
		public ListItemPageAdapter(List<Idxe> list,int position) {
			this.list = list;
			this.position = position;
		}
		
		@Override
		public int getCount() {
			return list.size();
		}

		@Override
		public boolean isViewFromObject(View view, Object object) {
			return view == object;
		}
		
		 @Override  
		    public void destroyItem(ViewGroup container, int position,Object object) {  
			 	container.removeView((View) object);
		    }  

		@Override
		public int getItemPosition(Object object) {
			return super.getItemPosition(object);
		}

		@Override
		public Object instantiateItem(ViewGroup container, int position) {
			PhotoView photoView = new PhotoView(container.getContext());
			Idxe each = list.get(position);
			HttpManager.getInstance().loadCommonImage(photoView, each.imgIdxName,new ImageLoadingListener() {
				@Override
				public void onLoadingStarted(String imageUri, View view) {
				}
				
				@Override
				public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
					mLoadingLayout.setVisibility(View.GONE);
				}
				
				@Override
				public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
					mLoadingLayout.setVisibility(View.GONE);
				}
				
				@Override
				public void onLoadingCancelled(String imageUri, View view) {
					mLoadingLayout.setVisibility(View.GONE);
				}
			});
			photoView.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					mPicDialog.dismiss();
				}
			});
			container.addView(photoView, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
			return photoView;
		}

		@Override
		public void onPageScrolled(int position, float positionOffset,
				int positionOffsetPixels) {
			// TODO Auto-generated method stub
		}

		@Override
		public void onPageSelected(int position) {
			changeSelectBg(position);
			
		}

		@Override
		public void onPageScrollStateChanged(int state) {
			// TODO Auto-generated method stub
		}
		
	}

	public void changeSelectBg(int position){
			if (mLinearLayout != null) {
				mLinearLayout.getChildAt(position).setSelected(true);
				mCurrentPicturePosition = position;
				int childCount = mLinearLayout.getChildCount();
				for (int i = 0; i < childCount; i++) {
					mLinearLayout.getChildAt(i).setSelected(i==position);
				}
			}
	} 
	public void fillPictures(List<Idxe> idxe,int position){
		long begin = System.currentTimeMillis();
		LogUtil.d("--begin picture time" + begin);
		int size = idxe.size();
		mLoadingLayout.setVisibility(View.VISIBLE);
		for (int i = 0; i < size; i++) {
			if (size > 1) {
				fillSelectLayout(mLinearLayout, i);
			}
		}
		mPageAdapter.setData();
		mViewPager.setCurrentItem(position);
	}
	int mIndexMargin = CommonUtil.dip2px(6);
	public void fillSelectLayout(LinearLayout layout,int position){
		ImageView image = new ImageView(BaseApplication.mContext);
		LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
		lp.setMargins(mIndexMargin, 0, mIndexMargin, 0);
		image.setImageResource(R.drawable.homepage_pager_selector);
		if (position == 0) {
			image.setSelected(true);
		}
		layout.addView(image, lp);
	}
	
	public void share(Community community){
		String mDefaultShareStr = BaseApplication.getResString(R.string.community_list_title);
		String mShareImg = "";
		if (community.idxes != null && community.idxes.size() > 0) {
			mShareImg = community.idxes.get(0).imgIdxName;
		}
		if(mShareDialog == null){
			dialogView = (LinearLayout) mInflater.inflate(R.layout.share_pop, null);
			mShareDialog = buildAlertDialog(dialogView,CommonUtil.dip2px(20));
		}
		if (mShareDialog.isShowing()) {
			return;
		}
		String title = TextUtils.isEmpty(community.title)?community.content:community.title;
		final String content = TextUtils.isEmpty(community.content)?mDefaultShareStr:community.content;
		if (TextUtils.isEmpty(title)) {
			title = mDefaultShareStr;
		}
		if (TextUtils.isEmpty(mShareImg) && community.isAnous.key == 0) {
			mShareImg = community.user.imgIdxName;
		}
		CommonUtil.showShareDialog(CONSTANT.ASSO_TYPE_COMMUNITY, dialogView,mShareDialog, mInflater, mShareImg, title, content,community.shareUrl);
	}
	
	Dialog mShareDialog;
	LinearLayout dialogView = null;
	
	@Override
	public void clear() {
		// TODO Auto-generated method stub
	}
	@Override
	public void onDestroy() {
		EventBus.getDefault().unregister(this);
		super.onDestroy();
	}

	@Override
	public void initArgs(Bundle args) {
		if (args != null) {
			mFragmentId = args.getInt(CONSTANT.ARGS.FRAGMENT_ID);
		}
	}

}
